<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Models\Orders;
use App\Models\Addr;
use App\Models\Orderstatu;

//使用类
use App\Http\Controllers\Controller;

//后台订单控制器
class OrdersController extends Controller
{
    //订单首页
    public function index(Request $request){

      $tot = Orders::count();
//echo "订单管理";exit;
        $data = Orders::select("orders.*","user.name","orderstatu.name as ssname")
            ->join("user","user.id","=","orders.uid")
           /* ->join("goods","goods.id","=","orders.gid")
            ->join("addr","addr.id","=","orders.aid")*/
            ->join("orderstatu","orderstatu.id","=","orders.sid")
            ->groupBy("orders.code")
            ->get();
/*
        $newArr=array();
        foreach($data as $key=>$value){
            $newArr[$value->code]=$value;
        }
        dd($newArr);
*/
        //加载页面
        return view("admin.orders.index")->with('data',$data)->with('tot',$tot);
    }

    //查看订单详情
    public function lists(Request $request){
        //获取订单号
        $code=$request->input('code');
        //echo $code;
        $data = Orders::select("orders.*","goods.title","goods.img")
        ->join("goods","goods.id","=","orders.gid")
            ->where("code",$code)->get();
        //dd($data);
        //数据展示到界面
        return view("admin.orders.lists")->with("data",$data);

    }

    //收货地址方法
    public function addr(Request $request){
        //获取数据
        $id = $request->input('id');
        //echo $id;
        $data = Addr::find($id);
        //dd($data);

        //加载页面
        return view("admin.orders.addr")->with("data",$data);

    }

    //订单状态的修改页面
    public function edit(Request $request){

        if($request->isMethod('post')){
            //dd($request->all());
            $sid = $request->input('sid');
            $code = $request->input('code');
            $sql = Orders::where('code',$code)->update(['sid'=>$sid]);

            if($sql){
                return redirect("admin/orders");
            }else{
              return back();
            }


        }else{



            //查询所有的订单状态
            $data= Orderstatu::get();

            return view('admin.orders.edit',[
                'data'=>$data
            ]);
        }


    }

    //订单状态列表的方法
    public function statuList(){

        //查询数据
        $data = Orderstatu::get();

        //加载页面
        return view("admin.orders.statuList")->with("data",$data);

    }

    //订单状态的修改
    public function statuEdit(Request $request){
       // header("content-type:text/html;charset=utf-8");
        //dd($request->all());
        $id = $request->input('id');
        $name = $request->input('name');
        $sql = Orderstatu::where('id',$id)->update(['name'=>$name]);
        //修改数据
        if($sql){
            return 1;
        }else{
            return 0;
        }
    }


	
}
